﻿using RoiControllers;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CSharp学习之ROI绘制功能实现
{
    public partial class MainForm : Form
    {
        private string imgSource = string.Empty;
        public MainForm()
        {
            InitializeComponent();
            this.CenterToParent();
            CheckForIllegalCrossThreadCalls = true;
        }
        private void MainForm_Load(object sender, EventArgs e)
        {

        }
        private void Btn_LoadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = Environment.CurrentDirectory;
            if (File.Exists(imgSource))
                openFile.InitialDirectory = Path.GetDirectoryName(imgSource);
            openFile.Filter = "(所有图像)|*.png;*.jpg;*.jpeg;*.bmp|(Png)|*.png";
            if (openFile.ShowDialog()== DialogResult.OK)
            {
                imgSource = openFile.FileName;
                uImageWindow.Image  = Image.FromFile(imgSource);
            }
        }
        private void Btn_DrawRect_Click(object sender, EventArgs e)
        {
            AddShape(new URectangle());
        }
        private void Btn_DrawRect2_Click(object sender, EventArgs e)
        {
            AddShape(new URectangle2());
        }
        private void Btn_DrawCircle_Click(object sender, EventArgs e)
        {
            AddShape(new URCircle());
        }
        private void AddShape(ShapeBase shape)
        {
            shape.OnAttach(OnAttchROI);
            shape.OnSelected(OnSelectedROI);
            shape.OnMove(OnMoveROI);
            shape.OnResize(OnResizeROI);
            shape.OnRotate(OnROtateROI);
            uImageWindow.AttachShapeToWindow(shape);
            uImageWindow.AddShape(shape);
        }
        private void PrintShapeData(ShapeBase shape)
        {
            double[] data;
            switch (shape.Type)
            {
                case ShapeType.Rectangle:
                    shape.GetTypeShape(shape.Type, out data);
                    UpdateMessage($"类型：{shape.Type},[X:{data[0]},Y:{data[1]},W:{data[2]},H:{data[3]}]");
                    return;
                case ShapeType.Rectangle2:
                    shape.GetTypeShape(shape.Type, out data);
                    UpdateMessage($"类型：{shape.Type},[X:{data[0]},Y:{data[1]},W:{data[2]},H:{data[3]},R:{data[4]}]");
                    return;
                case ShapeType.Circle:
                    shape.GetTypeShape(shape.Type, out data);
                    UpdateMessage($"类型：{shape.Type},[X:{data[0]},Y:{data[1]},Radio：{data[2]}]");
                    return;
                case ShapeType.Ellipse:
                    return;
                case ShapeType.Polygon:
                    return;
                case ShapeType.None:
                    return;
                default:
                    throw new ArgumentException("Unknown shape type");
            }
        }
        private void UpdateMessage(string message)
        {
            
            rtbx_Message.BeginInvoke(new Action(() =>
            {
                if (rtbx_Message.Lines.Length > 1000) rtbx_Message.Clear();
                if (rtbx_Message.InvokeRequired)
                {
                    rtbx_Message.BeginInvoke(new Action(() =>
                    {
                        rtbx_Message.AppendText($"{message}{Environment.NewLine}");
                        rtbx_Message.ScrollToCaret();
                    }));
                }
                else
                {
                    rtbx_Message.AppendText($"{message}{Environment.NewLine}");
                    rtbx_Message.ScrollToCaret();
                }
            }));
        }

        #region ROI事件回调
        private void OnAttchROI(ShapeBase shape, PointF f)
        {
            UpdateMessage($"添加形状，{shape.Description}=>{shape.Type}");
            PrintShapeData(shape);
        }
        private void OnMoveROI(ShapeBase shape, PointF f)
        {
            UpdateMessage($"移动形状，{shape.Description}=>{shape.Type}");
            PrintShapeData(shape);
        }
        private void OnResizeROI(ShapeBase shape, PointF f)
        {
            UpdateMessage($"调整形状大小，{shape.Description}=>{shape.Type}");
            PrintShapeData(shape);
        }
        private void OnROtateROI(ShapeBase shape, PointF f)
        {
            UpdateMessage($"旋转形状，{shape.Description}=>{shape.Type}");
            PrintShapeData(shape);
        }
        private void OnSelectedROI(ShapeBase shape, PointF f)
        {
            UpdateMessage($"选中形状，{shape.Description}=>{shape.Type}");
            PrintShapeData(shape);
        }
        #endregion

    }
}
